package cn.easy.jacob.utils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JacobUtil {

    private static Dispatch selection;

    public static Dispatch openDocument(ActiveXComponent app, String docPath, Boolean readOnly) {
        //设置word应用程序后台静默处理。
        app.setProperty("Visible", new Variant(false));
        //实例化文档
        Dispatch document = app.getProperty("Documents").toDispatch();
        //打开文档
        Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method,
                new Object[]{docPath, new Variant(true), new Variant(readOnly)}, new int[1]).toDispatch();
        selection = Dispatch.get(app, "Selection").toDispatch();
        return doc;
    }

    /**
     * 设置当前选定内容的字体
     *
     * @param bold  是否加粗
     * @param italic 是否斜体
     * @param underLine 下划线
     * @param colorSize 字体颜色
     * @param size      字体大小
     * @param name      字体名称
     */
    public static void setFont(boolean bold, boolean italic, boolean underLine,
                               String colorSize, String size, String name) {
        Dispatch font = Dispatch.get(selection, "Font").toDispatch();
        ;
        Dispatch.put(font, "Name", new Variant(name));
        Dispatch.put(font, "Bold", new Variant(bold));
        Dispatch.put(font, "Italic", new Variant(italic));
        Dispatch.put(font, "Underline", new Variant(underLine));
//        Dispatch.put(font, "Color", colorSize);
        Dispatch.put(font, "Size", size);
    }

    /**
     *
     * @param table
     * @param width 实际传参需乘以8
     */
    public static void setTableBorderWidth(Dispatch table, double width) {
        /*
         * 设置表格线的粗细 1：代表最上边一条线 2：代表最左边一条线 3：最下边一条线 4：最右边一条线 5：除最上边最下边之外的所有横线
         * 6：除最左边最右边之外的所有竖线 7：从左上角到右下角的斜线 8：从左下角到右上角的斜线
         */
        Dispatch borders = Dispatch.get(table, "Borders").toDispatch();
        for (int i = 1; i <= 4; i++) {
            Dispatch border = Dispatch.call(borders, "Item", new Variant(i)).toDispatch();
            Dispatch.put(border, "LineStyle", new Variant(1));
            Dispatch.put(border, "Visible", new Variant(true));
            Dispatch.put(border, "LineWidth", new Variant(width * 8));
        }
    }

    public static boolean find(String toFindText) {
        /* 从selection所在位置开始查询 */
        Dispatch find = Dispatch.call(selection, "Find").toDispatch();
        /* 设置要查找的内容 */
        Dispatch.put(find, "Text", toFindText);
        /* 向前查找 */
        Dispatch.put(find, "Forward", "True");
        /* 设置格式 */
        Dispatch.put(find, "Format", "True");
        /* 大小写匹配 */
        Dispatch.put(find, "MatchCase", "True");
        /* 全字匹配 */
        Dispatch.put(find, "MatchWholeWord", "True");
        /* 查找并选中 */
        return Dispatch.call(find, "Execute").getBoolean();
    }

    /**
     * 设置选定单元格的垂直对起方式
     * @param verticalAlign 0-顶端, 1-居中, 3-底端
     */
    public static void setCellVerticalAlign(int verticalAlign) {
        Dispatch cells = Dispatch.get(selection, "Cells").toDispatch();
        Dispatch.put(cells, "VerticalAlignment", new Variant(verticalAlign));
    }

    /**
     * 对当前段落进行格式化
     *
     * @param align     设置排列方式 默认：居左 0:居左 1:居中 2:居右 3:两端对齐 4:分散对齐
     * @param lineSpace 设置行间距 默认：1.0 0：1.0 1：1.5 2：2.0 3：最小值 4：固定值
     * @param firstLineIndent 首行缩进字符数
     */
    public static void setParaFormat(int align, int lineSpace, double firstLineIndent) {
        Dispatch alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch();
        Dispatch.put(alignment, "Alignment", align);
        Dispatch.put(alignment, "LineSpacingRule", new Variant(lineSpace));
        Dispatch.put(alignment, "CharacterUnitFirstLineIndent", new Variant(firstLineIndent));
    }

    public static void updateDirectory(Dispatch activeDocument) throws Exception {
        try {
            //获取目录
            Dispatch tablesOfContents = Dispatch.get(activeDocument, "TablesOfContents").toDispatch();
            Variant tablesOfContent = Dispatch.call(tablesOfContents, "Item", new Variant(1));

            /**更新目录
             *Update　更新域
             *UpdatePageNumbers　只更新页码
             */
            Dispatch toc = tablesOfContent.toDispatch();
            toc.call(toc, "Update");
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * @param alignment  1-居中,2-Right
     * @param heightRule 0-自动wdRowHeightAuto,1-最小值wdRowHeightAtLeast, 2-固定wdRowHeightExactly
     * @param height
     */
    public static void setTableFormat(Dispatch rows, int alignment, int heightRule, double height) {
        Dispatch.put(rows, "Alignment", new Variant(alignment));
        Dispatch.put(rows, "HeightRule", new Variant(heightRule));
        Dispatch.put(rows, "Height", new Variant(height * 28.35));
    }

//    public static void main(String[] args) throws Exception {
//        new JacobService().updateTable("E:\\说明.docx");
//    }
}
